﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Xcj.Mes.WholeLine.ZH.HG.Shell.Models
{
    /// <summary>
    /// 易损件数据模型
    /// </summary>
    public class WearPartData : INotifyPropertyChanged
    {
        private int _usedTimes;
        private int _limitTimes;
        private string _title;
        private string _unit = "次"; // 默认单位为"次"
        private int _changeCount;

        public string Title
        {
            get { return _title; }
            set { _title = value; OnPropertyChanged(nameof(Title)); }
        }

        public int UsedTimes
        {
            get { return _usedTimes; }
            set
            {
                _usedTimes = value;
                OnPropertyChanged(nameof(UsedTimes));
                OnPropertyChanged(nameof(Value));
                OnPropertyChanged(nameof(RemainderTimes));
                OnPropertyChanged(nameof(RemainderTimesContent));
                OnPropertyChanged(nameof(UsedTimesContent));
                OnPropertyChanged(nameof(LimitTimesContent));
            }
        }

        public int LimitTimes
        {
            get { return _limitTimes; }
            set
            {
                _limitTimes = value;
                OnPropertyChanged(nameof(LimitTimes));
                OnPropertyChanged(nameof(Value));
                OnPropertyChanged(nameof(RemainderTimes));
                OnPropertyChanged(nameof(RemainderTimesContent));
                OnPropertyChanged(nameof(UsedTimesContent));
                OnPropertyChanged(nameof(LimitTimesContent));
            }
        }

        public string Unit
        {
            get { return _unit; }
            set
            {
                _unit = value;
                OnPropertyChanged(nameof(Unit));
                OnPropertyChanged(nameof(RemainderTimesContent));
                OnPropertyChanged(nameof(UsedTimesContent));
                OnPropertyChanged(nameof(LimitTimesContent));
            }
        }

        public int ChangeCount
        {
            get { return _changeCount; }
            set
            {
                _changeCount = value;
                OnPropertyChanged(nameof(ChangeCount));
            }
        }

        public int Value => (int)((double)UsedTimes / LimitTimes * 360); // 映射到0-360度

        public int RemainderTimes => LimitTimes - UsedTimes;

        public string RemainderTimesContent => $"剩余{UnitDisplay}\n{RemainderTimes}";

        public string UsedTimesContent => $"{UsedTimes}";

        public string LimitTimesContent => $"限定使用{UnitDisplay}\n{LimitTimes}";

        /// <summary>
        /// 获取单位显示字符串
        /// </summary>
        private string UnitDisplay
        {
            get
            {
                switch (_unit)
                {
                    case "小时":
                        return "时间(h)";
                    case "次":
                        return "次数";
                    default:
                        return _unit;
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
